<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动匹配功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        input, select {
            width: 100%;
            padding: 8px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f0f0f0;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <h1>自动匹配功能测试</h1>
    
    <div class="test-section">
        <h3>测试数据</h3>
        <div id="testData"></div>
    </div>
    
    <div class="test-section">
        <h3>输入测试</h3>
        <label for="modelName">模型名称:</label>
        <input type="text" id="modelName" placeholder="输入模型名称，如：经典刀锋皮肤">
        <button onclick="testMatch()">测试匹配</button>
        <div id="matchResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>API测试</h3>
        <button onclick="testAPI()">测试API调用</button>
        <div id="apiResult" class="result"></div>
    </div>

    <script>
        // 模拟数据
        const modelIdData = [
            { m_id: 101011000, m_name: "经典刀锋皮肤", m_occupation: "bladePoint" },
            { m_id: 101011001, m_name: "血腥玛丽", m_occupation: "bladePoint" },
            { m_id: 101011002, m_name: "王女艾琳", m_occupation: "bladePoint" },
            { m_id: 101011003, m_name: "捕猎少女欧丽莎", m_occupation: "bladePoint" },
            { m_id: 101011004, m_name: "炫彩之影·曼尼莎", m_occupation: "bladePoint" },
            { m_id: 101011005, m_name: "泳衣刀锋", m_occupation: "bladePoint" },
            { m_id: 101011006, m_name: "灵魂圣女·柯莉丝汀", m_occupation: "bladePoint" },
            { m_id: 101011007, m_name: "烈焰女皇", m_occupation: "Flame" },
            { m_id: 101011008, m_name: "火焰舞者", m_occupation: "Flame" },
            { m_id: 101011009, m_name: "双枪手", m_occupation: "doubleGun" }
        ];

        // 显示测试数据
        document.getElementById('testData').innerHTML = `
            <p><strong>可用模型数据:</strong></p>
            <ul>
                ${modelIdData.map(item => `<li>${item.m_name} (ID: ${item.m_id}, 职业: ${item.m_occupation})</li>`).join('')}
            </ul>
        `;

        // 匹配函数
        function matchModelId(modelName) {
            if (!modelName || !modelIdData.length) {
                return null;
            }
            
            // 精确匹配
            let match = modelIdData.find(item => item.m_name === modelName);
            if (match) {
                return { type: 'exact', match };
            }
            
            // 模糊匹配
            match = modelIdData.find(item => 
                modelName.includes(item.m_name) || item.m_name.includes(modelName)
            );
            if (match) {
                return { type: 'fuzzy', match };
            }
            
            // 关键词匹配
            const keywords = modelName.split(/[\s\-·]+/);
            for (const keyword of keywords) {
                if (keyword.length > 1) {
                    match = modelIdData.find(item => 
                        item.m_name.includes(keyword)
                    );
                    if (match) {
                        return { type: 'keyword', match, keyword };
                    }
                }
            }
            
            return null;
        }

        // 测试匹配
        function testMatch() {
            const modelName = document.getElementById('modelName').value;
            const resultDiv = document.getElementById('matchResult');
            
            if (!modelName) {
                resultDiv.innerHTML = '<div class="error">请输入模型名称</div>';
                return;
            }
            
            const result = matchModelId(modelName);
            
            if (result) {
                const { type, match, keyword } = result;
                let typeText = '';
                switch (type) {
                    case 'exact': typeText = '精确匹配'; break;
                    case 'fuzzy': typeText = '模糊匹配'; break;
                    case 'keyword': typeText = `关键词匹配 (关键词: ${keyword})`; break;
                }
                
                resultDiv.innerHTML = `
                    <div class="success">
                        <strong>✅ 匹配成功!</strong><br>
                        匹配类型: ${typeText}<br>
                        模型名称: ${match.m_name}<br>
                        模型ID: ${match.m_id}<br>
                        职业: ${match.m_occupation}<br>
                        图片URL: https://model.lovemmd.cn/img/${match.m_id}.png
                    </div>
                `;
            } else {
                resultDiv.innerHTML = '<div class="error">❌ 无匹配结果</div>';
            }
        }

        // 测试API
        async function testAPI() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '<div>正在测试API...</div>';
            
            try {
                const response = await fetch('http://localhost:8080/api/v2/model/modelIds');
                const data = await response.json();
                
                if (data.code === 200) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ API调用成功!</strong><br>
                            状态码: ${response.status}<br>
                            返回数据条数: ${data.data.length}<br>
                            数据: ${JSON.stringify(data.data, null, 2)}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            ❌ API调用失败<br>
                            错误信息: ${data.msg}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        ❌ API调用异常<br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 回车键触发测试
        document.getElementById('modelName').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                testMatch();
            }
        });
    </script>
</body>
</html>
